(function () {
	var d={};
	//新增账号的时候，选择用户的时候
	jQuery('#submitBtn').on('click',function(){
		var id =$('#identify').val();
		var name=$('#fullname').val();
		
	});
	jQuery('#fileclick').on('click', function () {
		return  $("#file").click();
	});
	$("#file").change(function () {
	    var $file = $(this);
	    var fileObj = $file[0];
	    var windowURL = window.URL || window.webkitURL;
	    var dataURL;
	    var $img = $("#avatart");

	    if (fileObj && fileObj.files && fileObj.files[0]) {
	        dataURL = windowURL.createObjectURL(fileObj.files[0]);
	        $img.attr('src', dataURL);
	    } else {
	        dataURL = $file.val();
	        var imgObj = document.getElementById("preview");
	        // 两个坑:
	        // 1、在设置filter属性时，元素必须已经存在在DOM树中，动态创建的Node，也需要在设置属性前加入到DOM中，先设置属性在加入，无效；
	        // 2、src属性需要像下面的方式添加，上面的两种方式添加，无效；
	        imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
	        imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

	    }
	});
	$(document).ready(function() {
		$("#goHome").attr("href",$("#goHome").attr("href")+"&&identify="+$('#identify').val());
		$("#goHomeM").attr("href",$("#goHomeM").attr("href")+"&&identify="+$('#identify').val());
	});
    ///编辑
    jQuery('#saveBtn').on('click', function () {
    	
    	jQuery("#saveBtn").prop("disabled", true);
		$("#popupView").popup("open");
		
		    var url = window.rootPath +'/ui/bussinessEdit/saveDetailWithFile';
		    
		    	d.shopName=$("#shopName").val();
		    	d.shopAdress=$("#shopAdress").val();
		    	d.areaMeasure=$("#areaMeasure").val();
		    	d.signPerson=$("#signPerson").val();
		    	d.cleaningPerson=$("#cleaningPerson").val();
		    	d.managementRange=$("#managementRange").val();
		    	
		    	
		    	if(d.areaInfo!=undefined){
		    		d.areaInfo.id=$('#area').val();
		    	} else{
		    		d.areaInfo={"id":$('#area').val()};	
		    	}
		    	
		    	if(d.arreaManagementPerson!=undefined){
		    		d.arreaManagementPerson.id=$('#areaMperson').val();
		    	}else{
		    		d.arreaManagementPerson={"id":$('#areaMperson').val()};
		    	}
		    	d.checkDate =new Date();
		    	d.createTime =new Date();
		    	d.updateTime =new Date();
		    	
		    	var data={
		    			"id":d.id,
		    			"shopName":d.shopName,
		    			"shopAdress":d.shopAdress,
		    			"areaMeasure":d.areaMeasure,
		    			"signPerson":d.signPerson,
		    			"cleaningPerson":d.cleaningPerson,
		    			"infoState":d.infoState.id,
		    			"longitude":d.longitude,
		    			"latitude":d.latitude,
		    			"managementRange":d.managementRange,
		    			"areaInfo":d.areaInfo.id,//JSON.stringify({"id":d.areaInfo.id}),
		    			"arreaManagementPerson":d.arreaManagementPerson.id,//JSON.stringify({"id":d.arreaManagementPerson.id}),
		    		    "businesssellerBaseInfo":d.businesssellerBaseInfo.id//JSON.stringify({"id":d.businesssellerBaseInfo.id})
		    	}
		    	/*jQuery.ajax({
		            type: 'post',
		            url: encodeURI(url),
		            data: JSON.stringify(d),
		            dataType: 'json',
		            contentType: 'application/json;charset=utf-8',
		            success: function (data) {
		            	if(data.code==-1){
		            		message(data.message);
		            	}
		            	else{
		            		$("#popupView").popup();
		            		$("#popupView").popup('open');
		            		$("#test").trigger("click"); 
		            	}
		            	getData();
		            }
		        });*/
		    	$.ajaxFileUpload({
		    		url : url,
		    		secureuri : false,// 是否需要安全协议
		    		fileElementId : ["file"],// 文件上传的Id属性
		    		dataType : 'text',
		    		data : data,
		    		success : function(result, status) {
		    			jQuery("#saveBtn").prop("disabled", false);
		    			$("#popupView").popup("close");
		    			if (status=="success") {
		    				$("#popupView").popup();
		            		$("#popupView").popup('close');
		    			}
		    			getData();
		    			
		    		}
		    	});
	        
    });
    
    //提交
    jQuery('#submitBtn').on('click', function () {
    	jQuery("#submitBtn").prop("disabled", true);
		$("#popupView").popup("open");
	    var url = window.rootPath +'/ui/bussinessEdit/submitDetailWithFile';

	    	d.shopName=$("#shopName").val();
	    	d.shopAdress=$("#shopAdress").val();
	    	d.areaMeasure=$("#areaMeasure").val();
	    	d.signPerson=$("#signPerson").val();
	    	d.cleaningPerson=$("#cleaningPerson").val();
	    	d.managementRange=$("#managementRange").val();
	    	
	    	
	    	if(d.areaInfo!=undefined){
	    		d.areaInfo.id=$('#area').val();
	    	} else{
	    		d.areaInfo={"id":$('#area').val()};	
	    	}
	    	
	    	if(d.arreaManagementPerson!=undefined){
	    		d.arreaManagementPerson.id=$('#areaMperson').val();
	    	}else{
	    		d.arreaManagementPerson={"id":$('#areaMperson').val()};
	    	}
	    	/*d.areaInfo.id=$('#area').val();
	    	d.arreaManagementPerson.id=$('#areaMperson').val();*/
	    	
	    	var data={
	    			"id":d.id,
	    			"shopName":d.shopName,
	    			"shopAdress":d.shopAdress,
	    			"areaMeasure":d.areaMeasure,
	    			"signPerson":d.signPerson,
	    			"cleaningPerson":d.cleaningPerson,
	    			"infoState":d.infoState.id,
	    			"longitude":d.longitude,
	    			"latitude":d.latitude,
	    			"managementRange":d.managementRange,
	    			"areaInfo":d.areaInfo.id,//JSON.stringify({"id":d.areaInfo.id}),
	    			"arreaManagementPerson":d.arreaManagementPerson.id,//JSON.stringify({"id":d.arreaManagementPerson.id}),
	    		    "businesssellerBaseInfo":d.businesssellerBaseInfo.id//JSON.stringify({"id":d.businesssellerBaseInfo.id})
	    	}
	    	
	    	/*jQuery.ajax({
	            type: 'post',
	            url: encodeURI(url),
	            data: JSON.stringify(d),
	            dataType: 'json',
	            contentType: 'application/json;charset=utf-8',
	            success: function (data) {
	            	if(data.code==-1){
	            		message(data.message);
	            	}else
	            		{
	            		$("#popupView").popup();
	            		$("#popupView").popup('open');
	            		}
	            	getData();
	            }
	        });*/
	    	
	    	$.ajaxFileUpload({
	    		url : url,
	    		secureuri : false,// 是否需要安全协议
	    		fileElementId : ["file"],// 文件上传的Id属性
	    		dataType : 'text',
	    		data : data,
	    		success : function(result, status) {
	    			jQuery("#saveBtn").prop("disabled", false);
	    			$("#popupView").popup("close");
	    			if (status=="success") {
	    				$("#popupView").popup();
	            		$("#popupView").popup('close'); 
	    			}
	    			getData();
	    			
	    		}
	    	});
	    
        
});
    function getData(){
    	var identify =$("#identify").val();
        var url = window.rootPath +'/ui/bussinessEdit/getEditDetail/'+identify;
        jQuery.ajax({
            url:url,
            cache:false,
            type :"get",
            data:"",
            dataType:"json",
            contentType:'application/json;charset=utf-8',//解决返回数据中的中文乱码问题
            success:function(data){
            	d=data;
            	jQuery('#infoState').prop('readonly', 'readonly');
            	jQuery('#managementPerson').prop('readonly', 'readonly');
            	
            	if(data.infoState.title=="待认证")
            	{
            		jQuery('#cleaningPerson').prop('readonly', 'readonly');
            		jQuery('#managementRange').prop('readonly', 'readonly');
            		jQuery('#signPerson').prop('readonly', 'readonly');
            		jQuery('#shopAdress').prop('readonly', 'readonly');
            		jQuery('#pictureAdress').prop('readonly', 'readonly');
            		jQuery('#area').prop('readonly', 'readonly');
            		jQuery('#areaMperson').prop('readonly', 'readonly');
            		
            		jQuery("#submitBtn").attr("disabled", true);
            		}
            	
            	$("#shopName").val(data.shopName);
            	
            	var path ="";
        		if (data.infoState.title == '待认证') {
        			path="../img/authentication_in.png";
				} else if (data.infoState.title == '认证拒绝') {
					path="../img/authentication_notaccess.png";
				} else if (data.infoState.title == '认证通过') {
					path="../img/authentication_access.png";
				} else if (data.infoState.title == '初始化') {
					path="../img/authentication_init.png";
				} else if(data.infoState.title ==null){
					path ="";
				}
        		$("#infoState").attr('src',path); 
            	/*$("#infoState").val(data.infoState.title);*/
        		
        		var purl=data.pictureAdress;
				if(purl==undefined||purl==""){
					purl="../img/nopicture.png";
				}else{
					purl=window.rootPath+'/'+purl;
				}
				$("#avatart").attr("src",purl);
            	$("#shopAdress").val(data.shopAdress);
            	$("#areaMeasure").val(data.areaMeasure);
            	$("#signPerson").val(data.signPerson);
            	$("#cleaningPerson").val(data.cleaningPerson);
            	$("#managementRange").val(data.managementRange);
            	$("#managementPerson").val(data.managementPerson.name);
            	if(data.areaInfo!=undefined){
            		$('#area').val(data.areaInfo.id).selectmenu('refresh', true); 
            	}
            	if(data.arreaManagementPerson!=undefined){
            	$('#areaMperson').val(data.arreaManagementPerson.id).selectmenu('refresh', true); 
            	}
            	
            	//$("#area").find("option:contains('"+data.areaInfo.title+"')").attr("selected",true);
            	//jQuery('#area').val(data.areaInfo.id);
            	//$("#areaMperson").find("option:contains('"+data.arreaManagementPerson.name+"')").attr("selected",true);
            	//jQuery('#areaMperson').val(data.arreaManagementPerson.id);
            	
            	/*$("#managementPerson").html("管理人员: "+data.managementPerson.name); */
            }
        })
    };
    jQuery(document).ready(function () {
    	getData();
    });
})();